<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <link href="../css/frozen.css" rel="stylesheet">
    <link href="../css/mzui.css" rel="stylesheet">
</head>
<style type="text/css">
    .totalButtonGroup {
        position: absolute;
        bottom: 5px;
        text-align: center;
        line-height: 50px;
        width: 100%;
    }

    .carListCLass {
        border-radius: 13px;
        width: 100%;
        padding: 0 10%;
    }

    .codeStyle {
        line-height: 50px;
        text-align: center;
        color: #b9b5b5;
        font-size: 18px;
    }

    .ui-form-item {
        line-height: 60px;
        height: 60px;
    }
</style>

<body style="background: #fff;">
<div id="addGoodsApp">
    <div style="display:inline-block;position: absolute;width: 100%;height: 100%;">
        <div style="margin: 20px;line-height: 42px;text-align: center;" onclick="closeFomerView()">
            <i class="ui-icon-prev" style="float:left;    position: absolute;"></i>
            <span class="title">录入商品</span>
        </div>
        <div class="carListCLass">
            <div class="heading">
                <div class="title codeStyle">商品编号 {{good.code}}</div>
            </div>
            <div class="ui-form ui-border-t">
                <form action="#">
                    <div class="ui-form-item ui-border-b">
                        <label>商品名称</label>
                        <input type="text" v-model="good.name" placeholder="商品名称">
                    </div>
                    <div class="ui-form-item ui-border-b">
                        <label>商品描述</label>
                        <input type="text" v-model="good.desc" placeholder="商品描述">
                    </div>
                    <div class="ui-form-item ui-border-b">
                        <label>商品单价</label>
                        <input type="text" v-model="good.price" placeholder="商品单价">
                    </div>
                    <div class="ui-form-item ui-border-b">
                        <label>商品数量</label>
                        <input type="text" v-model="good.num" placeholder="商品数量">
                    </div>
                </form>
            </div>
            <div class="ui-btn-wrap" style="padding: 60px;text-align: center;">
                <img v-bind:src="good.url" onclick="upimg()" style="height:60px"/>
            </div>
        </div>
        <div class="totalButtonGroup">
            <button type="button" class="btn dark rounded" onclick="insertToDB()">录入</button>
            <button type="button" class="btn gray" style="margin-left:50px">重新扫描</button>
        </div>
    </div>
</div>
</body>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/CustomMath.js"></script>
<script type="text/javascript">
    var addGoodsApp = new Vue({
        el: '#addGoodsApp',
        data: {
            showPage: "none",
            carListHeight: "300px",
            good: {
                code: "123",
                url: "http://58pic.ooopic.com/58pic/13/76/84/40Q58PICAXw.jpg",
                num: null,
                price: null,
                desc: ""
            }
        }
    });
    var serverIp = "";
    apiready = function () {
        addGoodsApp.good.code = api.pageParam.goodCode;
        serverIP = api.loadSecureValue({
            sync: true,
            key: 'serverIP'
        });
    };
    apiready();


    function closeFomerView() {
        api.closeWin({
            name: 'addGoodsWin'
        });
    }

    //上传图片
    function upimg() {
        getPicture(1);
    }

    //获取图片
    function getPicture(buttonIndex) {
        var sourceType = "camera";
        if (buttonIndex == 2) {
            sourceType = "library";
        }
        api.getPicture({
            sourceType: sourceType,
            encodingType: 'jpg',
            mediaValue: 'pic',
            allowEdit: true,
            quality: 100,
            saveToPhotoAlbum: false
        }, function (ret, err) {
            addGoodsApp.good.url = ret.data;
        });
    }

    //插入数据库
    function insertToDB(){
        api.ajax({
            url: serverIp + '/good/addGoods',
            method: 'post',
            data: {
                values: {
                    goodInfo: addGoodsApp.good
                },
                files: {
                    file: addGoodsApp.good.url
                }
            }
        }, function(ret, err) {
            if (ret) {
                api.alert({ msg: JSON.stringify(ret) });
            } else {
                api.alert({ msg: JSON.stringify(err) });
            }
        });
    }
</script>

</html>
